#ifndef XGE_XUNITTEST_GRAPHICS2D_H_
#define XGE_XUNITTEST_GRAPHICS2D_H_
#include "graphics_window.h"
#include "g_graphics.h"
#include "xassert.h"
#include "xfs.h"
#include "xfsm.h"
#include "xge.h"
using namespace xge::window;
using namespace xge;
using namespace xge::graphics;

#include <windows.h>



UNIT_TEST(GraphicsTest,Case1){

  Graphics2D* interface_ = CreateGraphics2DDevice( NULL,800,600,false,_xT("TestingWindow") );

  FileSystemPtr file_system = XGameEngine::GetFileSystemManager()->GetFileSystem(_xT("Win32FileSystem"));
  XASSERT(!file_system.Null());
  XASSERT(interface_!=NULL);

  // Load a file directly
  FilePtr file = file_system->LoadFile(_xT("texture.png"));
  XASSERT(!file.Null());

  size_t length = (size_t)file->Length();

  char* mem_buffer = (char*)malloc( length*sizeof(char)+1 );

  file->Load(mem_buffer,length);

  mem_buffer[length*sizeof(char)] = '\0';

  Texture* texture = interface_->CreateTexture(mem_buffer,length,Color::kRed);
  XASSERT( texture != NULL );

  RenderTarget* target = interface_->CreateRenderTarget(32,32);
  XASSERT( target != NULL );

  RenderTexture* re_texture = target->GetTexture();

  MSG msg;

  GraphicsWindow* window = interface_->GetGraphicsWindow();

  // get the texture



  while( true ) {

    if( PeekMessage(&msg,NULL,0,0,PM_REMOVE) ) {
      if (msg.message == WM_QUIT)	 break ;
      TranslateMessage(&msg);
      DispatchMessage(&msg);
      continue;
    }

    interface_->BeginScene(Graphics2D::MODE_NORMAL,Graphics2D::PRIMITIVE_QUAD,target);
    interface_->Clear( Color::kRed );

    Quad quad;
    quad.texture = texture;
    quad.blending_type = BLEND_DEFAULT;
    for(int i=0;i<4;i++)
    {
      // Set up z-coordinate of vertices
      quad.vertex[i].z=0.5f;
      // Set up color. The format of DWORD col is 0xAARRGGBB
      quad.vertex[i].color=Color::kBlack.hardware_color();
    }

    // Set up quad's texture coordinates.
    // 0,0 means top left corner and 1,1 -
    // bottom right corner of the texture.

    quad.vertex[0].tx=96.0/128.0; quad.vertex[0].ty=64.0/128.0; 
    quad.vertex[1].tx=128.0/128.0; quad.vertex[1].ty=64.0/128.0; 
    quad.vertex[2].tx=128.0/128.0; quad.vertex[2].ty=96.0/128.0; 
    quad.vertex[3].tx=96.0/128.0; quad.vertex[3].ty=96.0/128.0; 
    quad.vertex[0].x=0; quad.vertex[0].y=0; 
    quad.vertex[1].x=32; quad.vertex[1].y=0; 
    quad.vertex[2].x=32; quad.vertex[2].y=32; 
    quad.vertex[3].x=0; quad.vertex[3].y=32; 
    interface_->RenderQuad(quad);

//     for( size_t i = 0 ; i <10000 ; ++i )
//     interface_->RenderLine(
//       Point(0.0f,0.0f) , Point(800.0f,600.0f) , 0.1f , BLEND_DEFAULT,
//       Color::kWhite,Color::kWhite
//       );
    interface_->EndScene();
    
    // rendering on the screen
    interface_->BeginScene();
    interface_->Clear(Color::kBlack);

    quad;
    quad.texture = re_texture;
    quad.blending_type = BLEND_DEFAULT;
    for(int i=0;i<4;i++)
    {
      // Set up z-coordinate of vertices
      quad.vertex[i].z=0.5f;
      // Set up color. The format of DWORD col is 0xAARRGGBB
      quad.vertex[i].color=0xFFFFA000;
    }

    // Set up quad's texture coordinates.
    // 0,0 means top left corner and 1,1 -
    // bottom right corner of the texture.

    quad.vertex[0].tx=96.0/128.0; quad.vertex[0].ty=64.0/128.0; 
    quad.vertex[1].tx=128.0/128.0; quad.vertex[1].ty=64.0/128.0; 
    quad.vertex[2].tx=128.0/128.0; quad.vertex[2].ty=96.0/128.0; 
    quad.vertex[3].tx=96.0/128.0; quad.vertex[3].ty=96.0/128.0; 
    quad.vertex[0].x=32; quad.vertex[0].y=32; 
    quad.vertex[1].x=64; quad.vertex[1].y=32; 
    quad.vertex[2].x=64; quad.vertex[2].y=64; 
    quad.vertex[3].x=32; quad.vertex[3].y=64; 
    interface_->RenderQuad(quad);
    interface_->EndScene();

  }

  
}


#endif// XGE_XUNITTEST_GRAPHICS2D_H_